#ifndef		__ENGINE_CSVFILE_H_INCLUDED__
#define		__ENGINE_CSVFILE_H_INCLUDED__

#include	"Strings.h"

/**
 * Helper class for parsing CSV file.
 */
class CsvFile {
public:
	CsvFile() {}

	/**
	 * Load a CSV file.
	 *
	 * \param	path		Path to find this file.
	 * \param	header		Which line is header.
	 * \param	delimiter	Data delimiter. Tab-key as default.
	 * \return	True for loaded successfully.
	 **/
	bool Load(const std::string & path, int header = 0, char delimiter = '\t');

	/**
	 * Get number of valid records in this file.
	 **/
	size_t Count() { return _data.size(); }

	/**
	 * Get all headers
	 **/
	std::vector<std::string> Headers() { return _header; }

	/**
	 * Get data in CSV file.
	 *
	 * \param	idx		Record index.
	 * \param	key		Key of value in this record
	 * \param	def		Default value if undefined.
	 * \return	Value of this data.
	 **/
	template<typename T>
	T Get(int idx, const std::string & key, const T & def) const {
		if (idx < 0 || (size_t)idx >= _data.size()) return def;
		for (size_t col = 0; col < _header.size(); ++col) {
			if (_header[col].compare(key) == 0 && _data[idx].size() > col) {
				std::string val = _data[idx][col];
				if (val.empty()) return def;
				return Parse<T>(val);
			}
		}
		return def;
	}

private:
	std::vector<std::string>				_header;
	std::vector<std::vector<std::string>>	_data;
};

#endif//!	__ENGINE_CSVFILE_H_INCLUDED__